package com.example.alios;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.ServiceException;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import static com.example.alios.Config.BUCKET_NAME;
import static com.example.alios.Config.OSS_ACCESS_KEY_ID;
import static com.example.alios.Config.OSS_ENDPOINT;

public class MainActivity extends AppCompatActivity implements UpFileListener {
     private UpCenter upCenter;
     private ImageView imageView;
     private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView=findViewById(R.id.imageTest);
        textView=findViewById(R.id.textTest);
        upCenter=new UpCenter(OSS_ENDPOINT, BUCKET_NAME,OSS_ACCESS_KEY_ID,getApplicationContext());
    }



    /**
     * 点击上传文件
     * @param view
     */
    public void onClick(View view){
//        上传单个文件啊
//        FileInformation fileInformation =new FileInformation("timg.jpg",Environment.getExternalStorageDirectory()+"/timg.jpg");
//        upCenter.upFile(fileInformation,this);
        //上传多个文件
        List<FileInformation> list=new ArrayList<>();
        list.add(new FileInformation("timg.jpg",Environment.getExternalStorageDirectory()+"/timg.jpg"));
        list.add(new FileInformation("123.jpg",Environment.getExternalStorageDirectory()+"/123.jpg"));
        upCenter.upFiles(list,this);
    }


    /**
     * 加载网络图片，获取网络图片的bitmap
     * @param url：网络图片的地址
     * @return
     */
    public static Bitmap getURLImage(String url) {
        Bitmap bmp = null;
        try {
            URL myurl = new URL(url);
            // 获得连接
            HttpURLConnection conn = (HttpURLConnection) myurl.openConnection();
            conn.setConnectTimeout(6000);//设置超时
            conn.setDoInput(true);
            conn.setUseCaches(true);//不缓存
            conn.connect();
            InputStream is = conn.getInputStream();//获得图片的数据流
            bmp = BitmapFactory.decodeStream(is);
            is.close();
            Log.e("测试","正常");

        } catch (Exception e) {
            Log.e("测试","报错");
            e.printStackTrace();
        }
        return bmp;
    }

    /**
     * 存储记录到数据库
     */
    public void saveData(OSSFile ossFile){

    }
    /**
     * 存储记录到数据库
     */
    public void saveData(List<OSSFile> files){

    }

    /**
     * 上传进度发生改变
     *
     * @param index       正在上传第几个文件
     * @param currentSize 当前上传量
     * @param totalSize   当前文件总大小
     */
    @Override
    public void onProgressChange(int index, long currentSize, long totalSize) {
        Log.d("PutObject", "index:"+index+"currentSize: " + currentSize + " totalSize: " + totalSize);
    }

    /**
     * 上传成功返回结果
     *
     * @param files
     */
    @Override
    public void onUpSuccess(List<OSSFile> files) {
        OSSFile ossFile=files.get(1);
        saveData(files);
        try {
            String url =upCenter.generateTempUrl(ossFile);
            Log.e("测试",url);
            Bitmap bitmap=getURLImage(url);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    imageView.setImageBitmap(bitmap);
                    textView.setText(url);
                }
            });

        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    /**
     * 上传失败触发
     *
     * @param clientException
     * @param serviceException
     */
    @Override
    public void onFaile(ClientException clientException, ServiceException serviceException) {
// 请求异常。
        if (clientException != null) {
            // 本地异常，如网络异常等。
            clientException.printStackTrace();
        }
        if (serviceException != null) {
            // 服务异常。
            Log.e("ErrorCode", serviceException.getErrorCode());
            Log.e("RequestId", serviceException.getRequestId());
            Log.e("HostId", serviceException.getHostId());
            Log.e("RawMessage", serviceException.getRawMessage());
        }
    }
}
